package com.sg.business.model.dataset;

import java.util.List;

import com.mobnut.db.model.PrimaryObject;
import com.mongodb.BasicDBObject;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.ProductItem;
import com.sg.business.model.Project;
import com.sg.business.model.SalesData;
import com.sg.widgets.commons.dataset.MasterDetailDataSetFactory;

public class SalesDataSet extends MasterDetailDataSetFactory {

	public SalesDataSet() {
		super(IModelConstants.DB, IModelConstants.C_SALESDATA);
	}

	@Override
	protected String getDetailCollectionKey() {
		return SalesData.F_MATERIAL_NUMBER;
	}
	
	@Override
	protected Object getMasterValue() {
		if(master instanceof Project){
			List<PrimaryObject> products = ((Project) master).getProduct();
			if(products==null||products.isEmpty()){
				return null;
			}
			String[] materialNumbers = new String[products.size()];
			for (int i = 0; i < materialNumbers.length; i++) {
				materialNumbers[i] = (String) products.get(i).getValue(ProductItem.F_DESC);
			}
			
			return new BasicDBObject().append("$in", materialNumbers);
		}
		
		return super.getMasterValue();
	}
	

}
